from xdg.Exceptions import ValidationError
from django.core.validators import EMPTY_VALUES, RegexValidator, URLValidator, EmailValidator, MaxLengthValidator, email_re, MinLengthValidator
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext, ugettext_lazy as _
import transsys

class RequiredValidator(object):
    message = _(u'Enter a valid value.')
    def __init__(self, message=None):
        if message:
            self.message = message

    def __call__(self, value):
        if value in EMPTY_VALUES:
            raise ValidationError(self.message)

class UniqueFactorValidator(object):
    message = _(u'Name of factors must be unique!')
    ts_program = None
    def __init__(self, ts_program, message=None):
        if isinstance(ts_program, transsys.TranssysProgram):
            self.ts_program = ts_program
        else:
            raise StandardError, 'transsys promgram %s is not valid' % str(ts_promgram)
        if message:
            self.message = message
    
    def __call__(self, value):
        for factor in self.ts_program.factor_list : 
            if value == factor.name : 
                raise ValidationError(self.message)

class UniqueGeneValidator(object):
    message = _(u'Name of genes must be unique!')
    ts_program = None
    def __init__(self, ts_program, message=None):
        if isinstance(ts_program, transsys.TranssysProgram):
            self.ts_program = ts_program
        else:
            raise StandardError, 'transsys promgram %s is not valid' % str(ts_promgram)
        if message:
            self.message = message
    
    def __call__(self, value):
        for gene in self.ts_program.gene_list : 
            if value == gene.name: 
                raise ValidationError(self.message)

class UniqueTranssysProgramValidator(object):
    message = _(u'Name of transsys programs must be unique!')
    ts_programs = None
    def __init__(self, ts_programs, message=None):
        self.ts_programs = ts_programs
        if message:
            self.message = message

    def __call__(self, value):
        for ts_program in self.ts_programs:
            if value == ts_program["tp"].name:
                raise ValidationError(self.message)

class ExpressionValueValidator(object):
    message = _(u"Expression value is invalid!")
    def __init__(self, message=None):
        if message:
            self.message = message
    def __call__(self, value):
        try:
            v = float(value)
        except:
            raise ValidationError(self.message)
        
def run_validators(value, validators):    
    for validator in validators:
        try:
            validator(value)
        except ValidationError, e:
            raise ValidationError(e.messages[0])
        
        